package com.example.retroiftdemo_729;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {

    private EditText etuser, etpass;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        etuser = findViewById(R.id.editText);
        etpass = findViewById(R.id.editText2);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://172.24.10.130:8080/foodService/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    UserService userService = retrofit.create(UserService.class);
                    Call<LoginBean> call = userService.login(etuser.getText().toString(),etpass.getText().toString());
                call.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        if(response.isSuccessful() && response.body()!= null&& response.body().getUserid()!=null )
                            Toast.makeText(LoginActivity.this,"登陆失败"+response.body().getUserid(), Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(LoginActivity.this,"网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
                }
        });
    }


}
